////////////////////////////////////////////////////////////////////////////////
//      
//      Kingsoft File for shp routines file
//      
//      File      : she_draw.h
//      Comment   : simple html parser draw class
//					
//      Create at : 2012-12-07 10:17:25
//      Create by : chenguicheng
//      
////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "shp_draw.h"

namespace shp_draw
{
#ifdef SHP_USE_GDIPLUS

	void frame_rect( shpdc& dc, CRect& rect, shpclr clr_border, int nAlpha )
	{
		Pen pen_draw(Color( nAlpha, GetRValue(clr_border), GetGValue(clr_border), GetBValue(clr_border)), 1);
		dc.DrawRectangle( &pen_draw, rect.left, rect.top, rect.Width(), rect.Height() );
	}

	void fill_rect( shpdc& dc, CRect& rect, shpclr clr_bg, int nAlpha )
	{
		SolidBrush brush( Color( nAlpha, GetRValue(clr_bg), GetGValue(clr_bg), GetBValue(clr_bg) ) );
		dc.FillRectangle( &brush, rect.left, rect.top, rect.Width(), rect.Height() );

	}

	void draw_text( shpdc& dc, shpstr& str_text, CRect& rect, UINT u_fmt, shpfont font, shpclr& clr_text, bool b_calc )
	{		
		StringFormat format;

		if( u_fmt & DT_CENTER )
			format.SetAlignment(StringAlignmentCenter);

		if( u_fmt& DT_RIGHT )
			format.SetAlignment(StringAlignmentCenter);

		if( u_fmt & DT_VCENTER )
			format.SetLineAlignment(StringAlignmentCenter);

		if( u_fmt & DT_BOTTOM )
			format.SetLineAlignment(StringAlignmentFar);

		if( u_fmt & DT_WORD_ELLIPSIS )
			format.SetTrimming( StringTrimmingEllipsisWord );

		if( u_fmt & DT_END_ELLIPSIS )
			format.SetTrimming( StringTrimmingEllipsisCharacter );

		if( u_fmt & DT_PATH_ELLIPSIS )
			format.SetTrimming( StringTrimmingEllipsisPath );


		RectF rc_draw( rect.left, rect.top, rect.Width(), rect.Height() );
		SolidBrush   solidBrush( Color(255, GetRValue(clr_text), GetGValue(clr_text), GetBValue(clr_text)) );
		dc.DrawString( str_text, str_text.GetLength(), font, rc_draw,&format, &solidBrush );

		if( b_calc )
		{
			RectF OutRect;
			dc.MeasureString( str_text, -1, font, rc_draw, &format, &OutRect );

			rect.left = OutRect.X;
			rect.top = OutRect.Y;
			rect.right = rect.left + ceil(OutRect.Width);
			rect.bottom = rect.top + ceil(OutRect.Height);
		}
		

	}

	shpfont create_new_font( BOOL b_bold, BOOL b_underline, BOOL b_italic, int n_size, shpstr& str_font )
	{
		int i_style = 0;
		if( b_bold )
			i_style = i_style | FontStyleBold;

		if( b_underline )
			i_style = i_style | FontStyleUnderline;

		if( b_italic )
			i_style = i_style | FontStyleItalic;

		shpfont p_font = new Font(	str_font, n_size, i_style, UnitPixel, NULL );
		return p_font;
	}

	void destroy_font( shpfont font )
	{
		if( font )
			delete font;
	}

	void redraw_rect( HWND h_wnd, CRect& rect )
	{
		::InvalidateRect( h_wnd, &rect, TRUE );
	}

	void destroy_img( shpimg& img )
	{
		if( img )
		{
			delete img;
			img = NULL;
		}
	}

	shpimg load_img( shpstr& str_img )
	{
		shpimg img = NULL;

		if ( str_img.GetLength() > 0 )
		{
			CPath str_path;
			str_path = shp_path_hlp::Instance().get_img_path();
			str_path.Append( str_img );

			if( str_path.FileExists() )
			{
				img = Image::FromFile( str_path );
			}
			else
			{
				assert( !L"File No Exists!" );
			}
		}

		return img;
	}

	CSize& get_img_size( shpimg img )
	{
		CSize size_info(0,0);
		if( img == NULL )
			return size_info;

		size_info.cx = img->GetWidth();
		size_info.cy = img->GetHeight();
		return size_info;
	}

	void draw_bmp( shpdc& dc, shpimg img, CRect& rc_bmp, CRect rc_draw, int i_alpha )
	{
		dc.SetInterpolationMode( InterpolationModeNearestNeighbor );
		dc.DrawImage( img, RectF( rc_draw.left, rc_draw.top, rc_draw.Width(), rc_draw.Height()),
						rc_bmp.left, rc_bmp.top, rc_bmp.Width()-0.5f, rc_bmp.Height()-0.5f, UnitPixel, NULL );
		
	}

	void draw_bmp( shpdc& dc, shpimg img, CRect& rc_bmp, int x_dest, int y_dest, int i_alpha )
	{
		dc.SetInterpolationMode( InterpolationModeNearestNeighbor );
		dc.DrawImage( img, RectF( x_dest, y_dest, rc_bmp.Width(), rc_bmp.Height()),
						rc_bmp.left, rc_bmp.top, rc_bmp.Width()-0.5f, rc_bmp.Height()-0.5f, UnitPixel, NULL );
	}

#else

	void frame_rect( shpdc& dc, CRect& rect, shpclr clr_border, int nAlpha )
	{
		CBrush brush;
		brush.CreateSolidBrush( clr_border );
		dc.FrameRect( &rect, brush );
	}

	void fill_rect( shpdc& dc, CRect& rect, shpclr clr_bg, int nAlpha )
	{
		dc.FillSolidRect( &rect, clr_bg );
	}

	void draw_text( shpdc& dc, shpstr& str_text, CRect& rect, UINT u_fmt, shpfont font, shpclr& clr_text, bool b_calc )
	{
		HFONT h_old_font = dc.SelectFont( font );
		int n_mode = dc.SetBkMode( TRANSPARENT );
		COLORREF clr_old = dc.SetTextColor( clr_text );

		dc.DrawText( str_text, -1, &rect, u_fmt );

		if( b_calc )
			dc.DrawText( str_text, -1, &rect, u_fmt | DT_CALCRECT );

		dc.SetTextColor( clr_old );
		dc.SetBkMode( n_mode );
		if( h_old_font )
			dc.SelectFont( h_old_font );
	}

	shpfont create_new_font( BOOL b_bold, BOOL b_underline, BOOL b_italic, int n_size, shpstr& str_font )
	{
		LOGFONT lfNew;
		memset( &lfNew, 0, sizeof(LOGFONT) );
		lfNew.lfWeight      = ( b_bold ? FW_BOLD : FW_NORMAL);
		lfNew.lfUnderline   = b_underline;
		lfNew.lfItalic      = b_italic;
		lfNew.lfHeight		= n_size;
		lfNew.lfQuality		= CLEARTYPE_QUALITY;
		_tcscpy( lfNew.lfFaceName, str_font );

		return ::CreateFontIndirect(&lfNew);
	}

	void destroy_font( shpfont font )
	{
		if( font )
			::DeleteObject( font );
	}

	void redraw_rect( HWND h_wnd, CRect& rect )
	{
		::InvalidateRect( h_wnd, &rect, TRUE );
	}

	void destroy_img( shpimg img )
	{
		if( img )
			::DeleteObject( HGDIOBJ(img) );
	}

	shpimg load_img( shpstr& str_img )
	{
		shpimg img = NULL;

		if ( str_img.GetLength() > 0 )
		{
			CPath str_path;
			str_path = shp_path_hlp::Instance().get_img_path();
			str_path.Append( str_img );

			if( str_path.FileExists() )
			{
				img = (shpimg)::LoadImage( _AtlBaseModule.GetResourceInstance(), str_path, IMAGE_BITMAP, 
					0, 0, LR_DEFAULTCOLOR | LR_LOADFROMFILE);
			}
			else
			{
				assert( !L"File No Exists!" );
			}
		}

		return img;
	}

	CSize& get_img_size( shpimg img )
	{
		CSize size_info(0,0);
		if( img == NULL )
			return size_info;

		BITMAP bmp;
		memset( &bmp, 0, sizeof(bmp) );
		::GetObject( img, sizeof(BITMAP), &bmp );

		size_info.cx = bmp.bmWidth;
		size_info.cy = bmp.bmHeight;
		return size_info;
	}

	void draw_bmp( shpdc& dc, shpimg img, CRect& rc_bmp, CRect rc_draw, int i_alpha )
	{
		if( rc_bmp.Width() == rc_draw.Width() && rc_bmp.Height() == rc_draw.Height() )
			_draw_bmp( dc, rc_bmp.left, rc_bmp.top, rc_bmp.Width(), rc_bmp.Height(), rc_draw.left, rc_draw.top, img );
		else
			_draw_bmp( dc, rc_bmp, rc_draw, img );
	}

	void draw_bmp( shpdc& dc, shpimg img, CRect& rc_bmp, int x_dest, int y_dest, int i_alpha )
	{
		_draw_bmp( dc, rc_bmp.left, rc_bmp.top, rc_bmp.Width(), rc_bmp.Height(), x_dest, y_dest, img );
	}

#endif

}